package com.rtech.ds.tree;

public class Traversal {

	public static void preOrder(Node root) {
		if(root != null) {
			System.out.print(" "+root.data);
			preOrder(root.leftChild);
			preOrder(root.rightChild);
		}
	} // end 9of preOrder
	
	public static void InOrder(Node root) {
		if(root != null) {
			InOrder(root.leftChild);
			System.out.print(" "+root.data);
			InOrder(root.rightChild);
		}
	} // end of inOrder
	
	public static void postOrder(Node root) {
		if(root != null) {
			postOrder(root.leftChild);
			postOrder(root.rightChild);
			System.out.print(" "+root.data);
		}
	} // end of postOrder
	
	
	public static void Min(Node root) {
		Node current = root;
		while(current.leftChild != null) {
			current = current.leftChild;
		}		
		System.out.print(" "+current.data);
	} // end of inOrder
	
	
	public static void Max(Node root) {
		Node current = root;
		while(current.rightChild != null) {
			current = current.rightChild;
		}		
		System.out.print(" "+current.data);
	} // end of inOrder
	
	public static void main(String[] args) {
		Node root = constructTree();
		System.out.println("\n preOrder Traversal >>> ");
		preOrder(root);
		
		System.out.println("\n\n InOrder Traversal >>> ");
		InOrder(root);
		
		System.out.println("\n\n postOrder Traversal >>> ");
		postOrder(root);
		
		System.out.println("\n\n Min value >>> ");
		Min(root);
		
		System.out.println("\n\n Max value >>> ");
		Max(root);
	}
	
	static Node constructTree() {
		Node c1 = new Node(20, null, null);
		Node c2 = new Node(40, null, null);
		Node b1 = new Node(30, c1, c2);
		Node b2 = new Node(60, null, null);
		Node a1 = new Node(50, b1, b2);
		return a1;
	} // end of construct node
}
